package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.zzyl.enums.BasicEnum.BED_DELETE_FAIL;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public void save(BedDto dto) {
        dto.setBedStatus(0);
        Bed bean = BeanUtil.toBean(dto, Bed.class);
        bedMapper.save(bean);
    }

    @Override
    public BedVo selectById(Long id) {
        Bed bed = bedMapper.selectById(id);
        BedVo bedVo = BeanUtil.toBean(bed, BedVo.class);
        return bedVo;
    }

    @Override
    public BedVo update(BedDto dto) {
        Bed bed = BeanUtil.toBean(dto, Bed.class);
        bedMapper.updateBed(bed);
        return BeanUtil.toBean(bed, BedVo.class);
    }

    @Override
    public void delete(Long id) {
        Bed bed = bedMapper.selectById(id);
        if (bed.getBedStatus()!=0){
            throw new BaseException(BasicEnum.BED_DELETE_FAIL);
        }
        bedMapper.deleteBedById(id);
    }


}

